package com.my.behavior.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my.behavior.mapper.ApArticleMapper;
import com.my.behavior.service.ApArticleService;
import com.my.model.article.pojos.ApArticle;
import com.my.model.common.dtos.ResponseResult;
import com.my.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
@Transactional
@Slf4j
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {
    /**
     * 读文章行为记录(阅读量+1)
     * @param map
     * @return
     */
    public ResponseResult readBehavior(Map map) {
        if(map == null || map.get("articleId") == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        Long articleId = (Long) map.get("articleId");
        ApArticle apArticle = getById(articleId);

        if(apArticle != null) {
            //获取文章运来阅读数
            Integer views = apArticle.getViews();

            //更新文章阅读数
            LambdaUpdateWrapper<ApArticle> luw = new LambdaUpdateWrapper<>();
            luw.eq(articleId != null,ApArticle::getId,articleId);
            luw.set(ApArticle::getViews,views + 1);
            update(luw);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
